package com.dkts.oil.util;

import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GeojsonUtil {
    public GeojsonUtil() {
    }

    public static Geometry GeometryToObj(String geometryJson) {
        GeometryJSON gjson = new GeometryJSON();
        Reader reader = new StringReader(geometryJson);
        Geometry geometry = null;

        try {
            geometry = gjson.read(reader);
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return geometry;
    }

    public static List<SimpleFeature> featuresToObj(List<String> featureStrs) {
        Reader reader = null;
        List<SimpleFeature> simpleFeatures = new ArrayList();
        Iterator var3 = featureStrs.iterator();

        while(var3.hasNext()) {
            String featureStr = (String)var3.next();
            reader = new StringReader(featureStr);
            FeatureJSON featureJSON = new FeatureJSON();

            try {
                SimpleFeature simpleFeature = featureJSON.readFeature(reader);
                simpleFeatures.add(simpleFeature);
            } catch (IOException var7) {
                var7.printStackTrace();
            }
        }

        return simpleFeatures;
    }

//    public static List<Feature> featureStringToFeatures(List<String> featureStrs) {
//        GeometryFactory geometryFactory = new GeometryFactory();
//        List<Feature> features = new ArrayList();
//        Iterator var3 = featureStrs.iterator();
//
//        while(var3.hasNext()) {
//            String featureStr = (String)var3.next();
//            Feature feature = GeoJsonFeatureConverter.fromGeoJsonFeature(featureStr, geometryFactory);
//            features.add(feature);
//        }
//
//        return features;
//    }

    public static String geoToJson(String wkt) {
        try {
            WKTReader reader = new WKTReader();
            Geometry geometry = reader.read(wkt);
            return geoToJsonByGeometry(geometry);
        } catch (Exception var6) {
            var6.printStackTrace();
        }
       return null;
    }
    public static String geoToJsonByGeometry(Geometry geometry) {
        String json = null;
        try {
            StringWriter writer = new StringWriter();
            GeometryJSON g = new GeometryJSON();
            g.write(geometry, writer);
            json = writer.toString();
        } catch (Exception var6) {
            var6.printStackTrace();
        }
        return json;
    }
    /**
     * 计算 geometry 周长（单位：米），默认认为 geometry 是 WGS84（经纬度）
     */
    public static double calculateLengthInMeters(Geometry geometry) {
        try {
            CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
            CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
            Geometry projected = JTS.transform(geometry, transform);
            return Math.round(projected.getLength() * 100.0) / 100.0;
        } catch (Exception e) {
            throw new RuntimeException("计算周长失败", e);
        }
    }
    public static String toGeoJson(List<Geometry> geometries) throws Exception {
        // 创建 Feature Type
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("Feature");
        builder.add("geometry", Geometry.class); // 也可以具体到 Polygon.class
        builder.setCRS(null); // 可以设置坐标系
        SimpleFeatureType featureType = builder.buildFeatureType();

        // 创建 FeatureJSON
        FeatureJSON fjson = new FeatureJSON();
        StringWriter writer = new StringWriter();

        writer.write("{\"type\":\"FeatureCollection\",\"features\":[");
        for (int i = 0; i < geometries.size(); i++) {
            Geometry geom = geometries.get(i);
            SimpleFeature feature = SimpleFeatureBuilder.build(
                    featureType,
                    new Object[]{geom},
                    String.valueOf(i)
            );

            fjson.writeFeature(feature, writer);
            if (i < geometries.size() - 1) {
                writer.write(",");
            }
        }
        writer.write("]}");
        return writer.toString();
    }

}